//
//  OPNoteDetailHeaderView.m
//  OnePiece
//
//  Created by lanou on 15/10/26.
//  Copyright © 2015年 wsy. All rights reserved.
//

#import "OPNoteDetailHeaderView.h"

@implementation OPNoteDetailHeaderView

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame])
    {
        self.backgroundColor = [UIColor clearColor];

        CGFloat imgH = SCREEN_HEIGHT / 2 - 40;
        CGFloat imgW = (SCREEN_HEIGHT / 2 - 40) * 640 / 436;

        
        _headerView = [[UIView alloc]initWithFrame:CGRectMake(SCREEN_WIDTH / 2 - imgW / 2, 0, imgW, imgH)];
        [self addSubview:_headerView];
        
        _userIcon = [[UIImageView alloc]initWithFrame:CGRectMake(10, SCREEN_HEIGHT / 4 - 20, 40, 40)];
        _userIcon.layer.cornerRadius = 20;
        _userIcon.layer.masksToBounds = YES;
        [self addSubview:_userIcon];
        
        _title = [[UILabel alloc]initWithFrame:CGRectMake(60, SCREEN_HEIGHT / 4 - 10, SCREEN_WIDTH - 80, 20)];
        _title.font = [UIFont fontWithName:@"Helvetica-Bold" size:17];
        _title.shadowOffset = CGSizeMake(0, 1);
        _title.shadowColor = [UIColor blackColor];
        _title.textColor = [UIColor whiteColor];
        [self addSubview:_title];
        
        _userName = [[UILabel alloc]initWithFrame:CGRectMake(60, SCREEN_HEIGHT / 4 + 20, SCREEN_WIDTH - 130, 15)];
        _userName.textColor = [UIColor whiteColor];
        _userName.font = [UIFont fontWithName:@"Helvetica-Bold" size:13];
        _userName.shadowColor = [UIColor blackColor];
        _userName.shadowOffset = CGSizeMake(0, 1);
        [self addSubview:_userName];
        
        _members = [[UILabel alloc]initWithFrame:CGRectMake(SCREEN_WIDTH - 70, SCREEN_HEIGHT / 4 + 20, 50, 20)];
        _members.backgroundColor = [UIColor colorWithWhite:0.182 alpha:1.000];
        _members.alpha = 0.6;
        _members.font = [UIFont fontWithName:@"Helvetica" size:13];
        _members.textColor = [UIColor whiteColor];
        _members.layer.cornerRadius = 5;
        _members.layer.masksToBounds = YES;
        [self addSubview:_members];
        
        _days = [[UILabel alloc]initWithFrame:CGRectMake(60, SCREEN_HEIGHT / 4 + 45, 150, 15)];
        _days.textColor = [UIColor whiteColor];
        _days.font = [UIFont fontWithName:@"Helvetica" size:13];
        [self addSubview:_days];
        
        _tags = [[UILabel alloc]initWithFrame:CGRectMake(60, SCREEN_HEIGHT / 4 + 70, SCREEN_WIDTH - 80, 15)];
        _tags.textColor = [UIColor whiteColor];
        _tags.font = [UIFont fontWithName:@"Helvetica" size:13];
        [self addSubview:_tags];
        
        _whiteView = [[UIView alloc]init];
        _whiteView.backgroundColor = [UIColor whiteColor];
        [self addSubview:_whiteView];
        
        _foreword = [[UILabel alloc]initWithFrame:CGRectMake(10, SCREEN_HEIGHT / 2 - 30, SCREEN_WIDTH - 10, 0)];
        _foreword.font = [UIFont systemFontOfSize:13];
        _foreword.numberOfLines = 0;
        _foreword.backgroundColor = [UIColor whiteColor];
        [self addSubview:_foreword];
    }
    return self;
}


- (void)configuredataWithModel:(OPNoteDetailHeaderModel *)m
{

    [self.userIcon sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://img.117go.com/demo27/img/ava102/%@",m.owner[@"avatar"]]]];
    self.title.text = m.title;
    self.userName.text = [NSString stringWithFormat:@"by: %@",m.owner[@"nickname"]];
    self.days.text = [NSString stringWithFormat:@"%@ | %ld天",m.startdate, m.days];
    self.tags.text = m.tags;
    self.members.text = [NSString stringWithFormat:@"成员%ld人",m.members.count];
    self.foreword.text = m.foreword;
    self.foreword.frame = CGRectMake(20, SCREEN_HEIGHT / 2 - 20, SCREEN_WIDTH - 40, [self heightForString:m.foreword]);
    self.whiteView.frame = CGRectMake(10, SCREEN_HEIGHT / 2 - 30, SCREEN_WIDTH - 20, [self heightForString:m.foreword] + 20);
}

- (CGFloat)heightForString:(NSString *)string
{
    NSDictionary *dic = [NSDictionary dictionaryWithObject:[UIFont systemFontOfSize:13] forKey:NSFontAttributeName];
    CGRect bound = [string boundingRectWithSize:CGSizeMake(SCREEN_WIDTH - 40, 0) options:(NSStringDrawingUsesLineFragmentOrigin) attributes:dic context:nil];
    return bound.size.height;
}


@end
